package com.hzc.springchan.bean.exception;

import com.hzc.springchan.enums.PubError;
import com.hzc.springchan.enums.PrivError;
import lombok.Getter;

/**
 * 自定义系统异常类
 * @author chen990627
 * @date 2022/09/06
 */
@Getter
public class CException extends RuntimeException {

    public final static String PUB_CODE = "springchan-01001";
    public final static String PUB_DESC = "系统异常，请稍后再试";

    /**
     * 对外异常错误码
     */
    private String pubCode;
    /**
     * 对外异常描述
     */
    private String pubDesc;
    /**
     * 对内异常错误码
     */
    private String privCode;

    public CException(PrivError privError) {
        this(privError, false);
    }

    public CException(PrivError privError, boolean userPrivDesc) {
        this(PUB_CODE, userPrivDesc ? privError.getErrorDesc() : PUB_DESC, privError.getErrorCode());
    }

    public CException(PubError pubError, PrivError privError) {
        this(PUB_CODE, pubError.getErrorDesc(), privError.getErrorCode());
    }

    public CException(String pubCode, String pubDesc, String privCode) {
        this.pubCode = pubCode;
        this.pubDesc = pubDesc;
        this.privCode = privCode;
    }

    @Override
    public String toString() {
        return "{SystemException: pubDesc -> " + pubDesc + ", privCode -> " + privCode + "}";
    }
}
